package unamed.entities;

import basis.game.entities.Entity;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import unamed.Art.Art;
import unamed.main.Unamed;

public class Blaster extends Entity{

    private BufferedImage image,image_overlay;
    private float fade=0,fade_rate=.0095f;
    public final static int color=255<<24|178<<16|34<<8|34;
    private double angle;
    private boolean charging;
    
    public Blaster(double x,double y){
        super(x,y,40,40);
        image=Art.scale(Art.blaster,40,10);
        image_overlay=Art.scale(Art.blaster_overlay,40,10);
        image_overlay=overlay(image_overlay);
    }
    
    public void fire(){
        if(charging){
            if(fade>=.9f){
                Unamed.blasts.add(new Blast(x+w/2-4+5*Math.cos(angle),y+h/2-4+5*Math.sin(angle),angle));
                fade=0;
                charging=false;
            }
        }else if(fade==0){
            Unamed.blasts.add(new Blast(x+w/2-2.5+5*Math.cos(angle),y+h/2-2.5+5*Math.sin(angle),angle));
            fade=1;
        }
    }
    
    public void charge(){
        charging=true;
    }
    
    private BufferedImage overlay(BufferedImage i){
        for(int x=0;x<40;x++){
            for(int y=0;y<10;y++){
                int pixel=i.getRGB(x,y);
                if((pixel>>24&0xff)>=100){
                    i.setRGB(x,y,color);
                }
            }
        }
        return i;
    }
    
    public void setAngle(double a){
        angle=a;
    }
    
    public double getAngle(){
        return angle;
    }
    
    @Override
    public void update(long t) {
        fade+=(charging)?fade_rate:-fade_rate;
        if(fade<0){
            fade=0;
        }
        if(fade>.9f){
            fade=.9f;
        }
    }

    @Override
    public void render(Graphics2D g){
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,fade));
        g.setColor(new Color(color).brighter());
        if(charging) g.fillOval((int)(x+w/2-(int)(8*fade)/2+(w/2-2)*Math.cos(angle)),(int)(y+h/2-(int)(8*fade)/2+(h/2-2)*Math.sin(angle)),(int)(8*fade),(int)(8*fade));
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1));
        g.drawImage(Art.rotate(image,angle+Math.PI,40,40),null,(int)x,(int)y);
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,fade));
        g.drawImage(Art.rotate(image_overlay,angle+Math.PI,40,40),null,(int)x,(int)y);
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1));
    }
    
}